/*
* Copyright 2025 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "lvgl.h"
#include <stdio.h>
#include "gui_guider.h"
#include "widgets_init.h"

#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "gg_external_data.h"
#endif

backlight_union_t backlight;
static uint16_t sta_buf[1] = {0};

void ui_init_style(lv_style_t * style)
{
    if (style->prop_cnt > 1)
        lv_style_reset(style);
    else
        lv_style_init(style);
}

void ui_load_scr_animation(lv_ui *ui, lv_obj_t ** new_scr, bool new_scr_del, bool * old_scr_del, ui_setup_scr_t setup_scr,
                           lv_scr_load_anim_t anim_type, uint32_t time, uint32_t delay, bool is_clean, bool auto_del)
{
    lv_obj_t * act_scr = lv_scr_act();

#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
    if(auto_del) {
        gg_edata_task_clear(act_scr);
    }
#endif
    if (auto_del && is_clean) {
        lv_obj_clean(act_scr);
    }
    if (new_scr_del) {
        setup_scr(ui);
    }
    lv_scr_load_anim(*new_scr, anim_type, time, delay, auto_del);
    *old_scr_del = auto_del;
}

void ui_animation(void * var, int32_t duration, int32_t delay, int32_t start_value, int32_t end_value, lv_anim_path_cb_t path_cb,
                  uint16_t repeat_cnt, uint32_t repeat_delay, uint32_t playback_time, uint32_t playback_delay,
                  lv_anim_exec_xcb_t exec_cb, lv_anim_start_cb_t start_cb, lv_anim_ready_cb_t ready_cb, lv_anim_deleted_cb_t deleted_cb)
{
    lv_anim_t anim;
    lv_anim_init(&anim);
    lv_anim_set_var(&anim, var);
    lv_anim_set_exec_cb(&anim, exec_cb);
    lv_anim_set_values(&anim, start_value, end_value);
    lv_anim_set_time(&anim, duration);
    lv_anim_set_delay(&anim, delay);
    lv_anim_set_path_cb(&anim, path_cb);
    lv_anim_set_repeat_count(&anim, repeat_cnt);
    lv_anim_set_repeat_delay(&anim, repeat_delay);
    lv_anim_set_playback_time(&anim, playback_time);
    lv_anim_set_playback_delay(&anim, playback_delay);
    if (start_cb) {
        lv_anim_set_start_cb(&anim, start_cb);
    }
    if (ready_cb) {
        lv_anim_set_ready_cb(&anim, ready_cb);
    }
    if (deleted_cb) {
        lv_anim_set_deleted_cb(&anim, deleted_cb);
    }
    lv_anim_start(&anim);
}

//要写入到mh FLASH的字符串数组
const uint8_t TEXT_Buffer[]={"MH FLASH TEST"};
#define SIZE sizeof(TEXT_Buffer)		//数组长度


uint16_t color_buf[1] = {0};


void Ui_Theme_Swtich(lv_ui *ui, uint8_t color)
{
    static uint8_t cur_theme;
    uint8_t datatemp[SIZE],i;
    if (color == WHITE) {
        guider_ui.color.backlight = BACKLIGHT_GREEN;
        ui_load_scr_animation(&guider_ui, &guider_ui.settings_1, guider_ui.settings_1_del, &guider_ui.main_del, setup_scr_settings_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        lv_group_focus_obj(ui->settings_1_btn_3);
    } else if (color == BLACK) {
        guider_ui.color.backlight = BACKLIGHT_BLACK;
        ui_load_scr_animation(&guider_ui, &guider_ui.settings_1, guider_ui.settings_1_del, &guider_ui.main_del, setup_scr_settings_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        lv_group_focus_obj(ui->settings_1_btn_5);
    } else if (color == RED) {
        guider_ui.color.backlight = BACKLIGHT_RED;
        ui_load_scr_animation(&guider_ui, &guider_ui.settings_1, guider_ui.settings_1_del, &guider_ui.main_del, setup_scr_settings_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        lv_group_focus_obj(ui->settings_1_btn_4);
    } else if (color == BLUE) {
        guider_ui.color.backlight = BACKLIGHT_BLUE;
        ui_load_scr_animation(&guider_ui, &guider_ui.settings_1, guider_ui.settings_1_del, &guider_ui.main_del, setup_scr_settings_1, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
        lv_group_focus_obj(ui->settings_1_btn_6);
    } else if(color == HOME) {
        guider_ui.color.backlight = BACKLIGHT_BLACK;
        ui_load_scr_animation(&guider_ui, &guider_ui.main, guider_ui.main_del, NULL, setup_scr_main, LV_SCR_LOAD_ANIM_NONE, 0, 0, false, true);
    }


    if (cur_theme != color) {
        cur_theme = color;
        // TEXT_Buffer[0] = cur_theme;
        // MHFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)TEXT_Buffer,SIZE);
        // MHFLASH_Read(FLASH_SAVE_ADDR,(uint16_t*)datatemp,SIZE);

        color_buf[0] = (uint16_t)cur_theme;
        MHFLASH_Write(FLASH_SAVE_ADDR,(uint16_t*)color_buf,1);

        memset(datatemp,0x00, sizeof(datatemp));
    }

}

void init_scr_del_flag(lv_ui *ui)
{

    ui->main_del = true;
    ui->motion_1_del = true;
    ui->motion_2_del = true;
    ui->settings_1_del = true;
    ui->light_1_del = true;
    ui->low_bat_del = true;
    ui->enter_pair_del = true;
    ui->massage_1_del = true;
    ui->massage_onoff_del = true;
    ui->massage_time_del = true;
    ui->massage_mode_del = true;
    ui->massage_intensity_del = true;
}



void setup_ui(lv_ui *ui)
{
    init_scr_del_flag(ui);
    init_keyboard(ui);
    setup_scr_massage_intensity(ui);
    MHFLASH_Read(FLASH_SAVE_ADDR, (uint16_t*)sta_buf, 1);
    Ui_Theme_Swtich(&guider_ui, (uint8_t)sta_buf[0]);
    setup_scr_main(ui);
    // setup_scr_massage_mode(ui);
    // setup_scr_low_bat(ui);
    // setup_scr_motion_2(ui);
    // setup_scr_massage_mode(ui);
    // lv_scr_load(ui->massage_mode);
    // setup_scr_enter_pair(ui);
    // lv_scr_load(ui->enter_pair);
    lv_scr_load(ui->main);
    // 初始化界面时注册 timer
    // 增加定时器优先级并传递UI指针
    lv_timer_create(update_ui_timer_cb, 200, ui);
    memset(&backlight, 0, sizeof(backlight_union_t));
}


void init_keyboard(lv_ui *ui)
{

}




void update_ui_timer_cb(lv_timer_t *timer)
{
    Setup_Rx_Sta(&guider_ui);
}


void Pair_Txt_Sta(lv_ui *ui, uint8_t sta)
{
    if (ui->enter_pair_1_label != NULL) {
        if (sta) {
            lv_label_set_text(ui->enter_pair_1_label, "Updated successfully!");
        } else {
            lv_label_set_text(ui->enter_pair_1_label, "Update failed!");
        }
    }
}

void Setup_Rx_Sta(lv_ui *ui)
{
    static uint32_t rx_sta = 0;
    if (ui == NULL) return; // 检查UI指针有效性

    // 进入临界区保护共享资源访问
    __disable_irq();
    uint32_t current_sta = backlight.sta;
    __enable_irq();

    if (rx_sta != current_sta) {
        // 检查当前活动屏幕指针有效性
        lv_obj_t *act_scr = lv_scr_act();
        if (act_scr == NULL) {
            rx_sta = current_sta;
            return;
        }

        #if 1
        __disable_irq();
        bool msg_mode1 = backlight.bits.msg_mode1;
        bool msg_mode2 = backlight.bits.msg_mode2;
        bool msg_mode3 = backlight.bits.msg_mode3;
        bool msg_mode4 = backlight.bits.msg_mode4;
        __enable_irq();
        if (msg_mode1 && (act_scr == guider_ui.massage_mode)) {
            if (ui->massage_mode_roller_1 != NULL) // 检查控件指针
                lv_roller_set_selected(ui->massage_mode_roller_1, 0, LV_ANIM_ON);
        } else if (msg_mode2 && (act_scr == guider_ui.massage_mode)) {
            if (ui->massage_mode_roller_1 != NULL)
                lv_roller_set_selected(ui->massage_mode_roller_1, 1, LV_ANIM_ON);
        } else if (msg_mode3 && (act_scr == guider_ui.massage_mode)) {
            if (ui->massage_mode_roller_1 != NULL)
                lv_roller_set_selected(ui->massage_mode_roller_1, 2, LV_ANIM_ON);
        } else if (msg_mode4 && (act_scr == guider_ui.massage_mode)) {
            if (ui->massage_mode_roller_1 != NULL)
                lv_roller_set_selected(ui->massage_mode_roller_1, 3, LV_ANIM_ON);
        }


        __disable_irq();
        bool msg_min10 = backlight.bits.msg_min10;
        bool msg_min20 = backlight.bits.msg_min20;
        bool msg_min30 = backlight.bits.msg_min30;
        __enable_irq();
        if (msg_min10 && (act_scr == guider_ui.massage_time)) {
            if (ui->massage_time_roller_1 != NULL)
                lv_roller_set_selected(ui->massage_time_roller_1, 0, LV_ANIM_ON);
        } else if (msg_min20 && (act_scr == guider_ui.massage_time)) {
            if (ui->massage_time_roller_1 != NULL)
                lv_roller_set_selected(ui->massage_time_roller_1, 1, LV_ANIM_ON);
        } else if (msg_min30 && (act_scr == guider_ui.massage_time)) {
            if (ui->massage_time_roller_1 != NULL)
                lv_roller_set_selected(ui->massage_time_roller_1, 2, LV_ANIM_ON);
        }
        #endif

        if (act_scr == guider_ui.massage_onoff) {
            if (ui->massage_onoff_btn_1_label != NULL) { // 检查标签指针
                __disable_irq();
            bool msg_on_off = backlight.bits.msg_on_off;
            __enable_irq();
            if (msg_on_off) {
                    lv_label_set_text(ui->massage_onoff_btn_1_label, "OFF");
                } else {
                    lv_label_set_text(ui->massage_onoff_btn_1_label, "ON");
                }
            }
        }

        #if 1
        __disable_irq();
        bool msg_foot_ins0 = backlight.bits.msg_foot_ins0;
        bool msg_foot_ins1 = backlight.bits.msg_foot_ins1;
        bool msg_foot_ins2 = backlight.bits.msg_foot_ins2;
        bool msg_intensity_foot = guider_ui.msg_intensity_pos.foot;
        __enable_irq();
        if (msg_foot_ins0 && msg_intensity_foot && (act_scr == guider_ui.massage_intensity)) {
            if (ui->massage_time_roller_1 != NULL && ui->massage_intensity_label_1 != NULL) {
                lv_roller_set_selected(ui->massage_time_roller_1, 0, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
            }
        } else if (msg_foot_ins1 && msg_intensity_foot && (act_scr == guider_ui.massage_intensity)) {
            if (ui->massage_time_roller_1 != NULL && ui->massage_intensity_label_1 != NULL) {
                lv_roller_set_selected(ui->massage_time_roller_1, 1, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
            }
        } else if (msg_foot_ins2 && msg_intensity_foot && (act_scr == guider_ui.massage_intensity)) {
            if (ui->massage_time_roller_1 != NULL && ui->massage_intensity_label_1 != NULL) {
                lv_roller_set_selected(ui->massage_time_roller_1, 2, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Foot Strength");
            }
        }

        __disable_irq();
        bool msg_head_ins0 = backlight.bits.msg_head_ins0;
        bool msg_head_ins1 = backlight.bits.msg_head_ins1;
        bool msg_head_ins2 = backlight.bits.msg_head_ins2;
        bool msg_intensity_foot_head = guider_ui.msg_intensity_pos.foot;
        __enable_irq();
        if (msg_head_ins0 && !msg_intensity_foot_head && (act_scr == guider_ui.massage_intensity)) {
            if (ui->massage_time_roller_1 != NULL && ui->massage_intensity_label_1 != NULL) {
                lv_roller_set_selected(ui->massage_time_roller_1, 0, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
            }
        } else if (msg_head_ins1 && !msg_intensity_foot_head && (act_scr == guider_ui.massage_intensity)) {
            if (ui->massage_time_roller_1 != NULL && ui->massage_intensity_label_1 != NULL) {
                lv_roller_set_selected(ui->massage_time_roller_1, 1, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
            }
        } else if (msg_head_ins2 && !msg_intensity_foot_head && (act_scr == guider_ui.massage_intensity)) {
            if (ui->massage_time_roller_1 != NULL && ui->massage_intensity_label_1 != NULL) {
                lv_roller_set_selected(ui->massage_time_roller_1, 2, LV_ANIM_ON);
                lv_label_set_text(ui->massage_intensity_label_1, "Head Strength");
            }
        }
        #endif

        if (act_scr == guider_ui.light_1) {
            if (ui->light_1_label_1 != NULL) { // 检查标签指针
            __disable_irq();
            bool led_state = backlight.bits.led;
            __enable_irq();
            if (led_state) {
                    lv_label_set_text(ui->light_1_label_1, "OFF");
                }  else {
                    lv_label_set_text(ui->light_1_label_1, "ON");
                }
            }
        }
        rx_sta = current_sta;
    }

}
